package com.zh.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.zh.constants.Constants;
import com.zh.mapstruct.OrderStructMapper;
import com.zh.pojo.domain.Order;
import com.zh.pojo.domain.OrderProfitsharing;
import com.zh.mapper.OrderProfitsharingMapper;
import com.zh.pojo.result.ProfitSharingResult;
import com.zh.pojo.vo.LoginVo;
import com.zh.service.IOrderProfitsharingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 平台司机分账表 服务实现类
 * </p>
 *
 * @author 初四
 * @since 2024-06-29
 */
@Service
public class OrderProfitsharingServiceImpl extends ServiceImpl<OrderProfitsharingMapper, OrderProfitsharing> implements IOrderProfitsharingService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void createSharing(ProfitSharingResult sharingResult, Order order) {
        OrderProfitsharing profitsharing = OrderStructMapper.INSTANCE.orderAndprofitSharingConvertOrderProfitsharing(order, sharingResult);
        // 分账状态，0未分账
        profitsharing.setStatus(Constants.Bill.SHARING_STATUS_ZERO);
        profitsharing.setCreateTime(new Date());
        // 获取分账人微信ID
        String loginInfoKey = String.format(Constants.Redis.LOGIN_INFO_KEY, StpUtil.getLoginIdAsLong());
        LoginVo login = (LoginVo) redisTemplate.opsForValue().get(loginInfoKey);
        profitsharing.setToUserOpenId(login.getOpenId());
        super.save(profitsharing);
    }
}
